package com.nanyang.tms.common.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.ByteArrayOutputStream;

public class StreamRegistry {

    public static void close( PdfStamper pdfStamper, PdfReader pdfReader) {
        if (pdfStamper != null) {
            try {
                pdfStamper.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pdfReader != null) {
            try {
                pdfReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void closePdf(Document document, PdfCopy pdfCopy, PdfReader pdfReader, ByteArrayOutputStream out) {
        if (document != null) {
            try {
                document.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pdfCopy != null) {
            try {
                pdfCopy.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pdfReader != null) {
            try {
                pdfReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeDocument(Document document, PdfCopy pdfCopy) {
        if (document != null) {
            try {
                document.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (pdfCopy != null) {
            try {
                pdfCopy.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
